package org.scc.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.model.Group;
import org.scc.model.MeetingPlace;
import org.scc.model.MeetingPlan;
import org.scc.model.User;
import org.scc.service.GroupService;
import org.scc.service.MeetingPlaceService;
import org.scc.service.MeetingPlanService;

public class MeetingPlanAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private MeetingPlaceService meetingPlaceService;
	private GroupService groupService;
	private MeetingPlanService meetingPlanService;
	
	private List<MeetingPlace> meetingPlaces;
	private List<String> dates;
	private int month;
	private Map<String, Integer> types;
	private Map<String, List<User>> groupUsers;
	
	private String date;
	private int type;
	private int meetingPlaceId;
	
	private String opt ;
	
	private String plans;
	
	public MeetingPlanAction(){
		meetingPlaceService = new MeetingPlaceService();
		groupService = new GroupService();
		meetingPlanService = new MeetingPlanService();
		month = Calendar.getInstance().get(Calendar.MONTH);
	}
	
	public String initPlanPage(){
		meetingPlaces = meetingPlaceService.findAll();
		types = MeetingPlan.getAllTypes();
		dates = getSundays(month);
		
		groupUsers = new HashMap<String, List<User>>();
		List<Group> groups = groupService.findByType(Group.WORSHIPGROUP);
		for(Group group : groups){
			groupUsers.put(group.getName(), groupService.getUsers(group.getId()));
		}
		
		return opt;
	}
	
	private List<String> getSundays(int month){
		List<String> dateList = new ArrayList<String>();
		//一个月的开始时间
		Calendar startCal = Calendar.getInstance();
		startCal.set( startCal.get(Calendar.YEAR), month, 1, 0, 0, 0);
		//一个月的结束时间
		Calendar endCal = Calendar.getInstance();
		endCal.set(Calendar.MONTH, month);
		int MaxDay = endCal.getActualMaximum(Calendar.DAY_OF_MONTH);
		endCal.set( endCal.get(Calendar.YEAR), month, MaxDay, 23, 59, 59);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.WEEK_OF_MONTH, 1);
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		for(int i = 1; i < 7 ; i++){
			calendar.add(Calendar.DAY_OF_MONTH, 7);
			if(calendar.after(startCal) && calendar.before( endCal ) ){
				dateList.add( sdf.format(calendar.getTime()) );
			}
		}
		return dateList;
	}
	
	public void initMeetingPlan(){
		List<User> users = meetingPlanService.findMeetingPlanUser(date, type, meetingPlaceId);
		responeString(getUserJSON(users));
	}
	
	private String getUserJSON(List<User> users){
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		for(User u : users){
			obj = new JSONObject();
			obj.put("id", u.getId());
			obj.put("name", u.getName());
			
			arr.add(obj);
		}
		return arr.toString();
	}
	
	public void saveMeetingPlans() throws Exception{
		String planArr[] = plans.split(";");
		String planInfo[] = null;
		for(String plan : planArr){
			planInfo = plan.split("&");
			//只有是4的时候才是正确的格式date  meetingPlace type userId
			if(planInfo.length == 4){
				meetingPlanService.deletePlan(planInfo[0], 
												Integer.parseInt(planInfo[2]), 
												Integer.parseInt(planInfo[1]) );
				meetingPlanService.addMeetingPlan(planInfo[0], 
						Integer.parseInt(planInfo[2]), 
						Integer.parseInt(planInfo[1]), 
						planInfo[3]);
			} else if(planInfo.length == 3){
				meetingPlanService.deletePlan(planInfo[0], 
						Integer.parseInt(planInfo[2]), 
						Integer.parseInt(planInfo[1]) );
			}
		}
		responseSuccess();
	}

	public List<MeetingPlace> getMeetingPlaces() {
		return meetingPlaces;
	}

	public void setMeetingPlaces(List<MeetingPlace> meetingPlaces) {
		this.meetingPlaces = meetingPlaces;
	}

	public List<String> getDates() {
		return dates;
	}

	public void setDates(List<String> dates) {
		this.dates = dates;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public Map<String, Integer> getTypes() {
		return types;
	}

	public void setTypes(Map<String, Integer> types) {
		this.types = types;
	}

	public Map<String, List<User>> getGroupUsers() {
		return groupUsers;
	}

	public void setGroupUsers(Map<String, List<User>> groupUsers) {
		this.groupUsers = groupUsers;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getMeetingPlaceId() {
		return meetingPlaceId;
	}

	public void setMeetingPlaceId(int meetingPlaceId) {
		this.meetingPlaceId = meetingPlaceId;
	}

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}

	public String getPlans() {
		return plans;
	}

	public void setPlans(String plans) {
		this.plans = plans;
	}
	
}
